export function getFileName(headers = {}) {
  const  { 'content-disposition': contentDisposition = '' } = headers
  let filename = contentDisposition.split('filename=')[1]
  filename = decodeURI(filename);
  
  return filename
}

export function createObjectURL(data) {
  const url = window.URL.createObjectURL(new Blob([data]));
  return url;
}

export function download(res) {
  const { data,headers} = res || {};
  const filename = getFileName(headers);
  // 文件导出
  if (!data) {
    return;
  }
  const url = window.URL.createObjectURL(new Blob([data]));
  let link = document.getElementById('download');
  if (!link) {
    link = document.createElement('a');
  }
  link.href = url;
  link.setAttribute('download', filename);
  if (!link) {
    link.style.display = 'none';
    link.setAttribute('id', 'download');
    document.body.appendChild(link);
  }
  link.click();
}